﻿// 功能：ImageContainer类的.Net 封装
// 描述：增加LoadImage函数
// 作者：温伟鹏
// 日期：2010/5/13 13:33:03

using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.IO;

namespace MeizuSDK.Drawing
{
    /// <summary>
    /// 图像容器，用于缓存已加载的图像
    /// </summary>
    public class ImageContainer : IDisposable
    {
        #region DllImport
        [DllImport("MZFCWrapper.dll")]
        private static extern IntPtr ImageContainerCtor();

        [DllImport("MZFCWrapper.dll")]
        private static extern void ImageContainerDispose(IntPtr pObj);

        [DllImport("MZFCWrapper.dll")]
        private static extern IntPtr ImageContainerLoadImage(IntPtr pObj, IntPtr hInstance, int nResID, bool bUseAlpha);

        [DllImport("MZFCWrapper.dll")]
        private static extern void ImageContainerRemoveAll(IntPtr pObj);

        [DllImport("MZFCWrapper.dll")]
        private static extern void ImageContainerRemoveImage(IntPtr pObj, IntPtr hInst, int nResID, bool bUseAlpha);
        #endregion

        #region 私有变量
        IntPtr pObj;
        /// <summary>
        /// 图像缓存
        /// </summary>
        Dictionary<string, ImagingHelper> images;
        #endregion

        #region 构造函数
        /// <summary>
        /// 构造图像容器
        /// </summary>
        public ImageContainer()
        {
            pObj = ImageContainerCtor();

            images = new Dictionary<string, ImagingHelper>();
        }
        #endregion

        #region 函数
        /// <summary>
        /// 从文件中加载图片
        /// </summary>
        /// <param name="fileName"></param>
        /// <returns>如果返回null，可能是文件未存在</returns>
        public ImagingHelper LoadImage(string fileName)
        {
            return LoadImage(fileName, false);
        }
        /// <summary>
        /// 从文件加载图片
        /// </summary>
        /// <param name="fileName">图像文件所在路径</param>
        /// <param name="reload">如果缓存中存在指定的图像，是否重新加载</param>
        /// <returns></returns>
        public ImagingHelper LoadImage(string fileName, bool reload)
        {
            fileName = fileName.ToLower();

            if (!File.Exists(fileName))
                return null;

            lock (images)
            {
                if (images.ContainsKey(fileName))
                {
                    if (reload)
                    {
                        ImagingHelper img = new ImagingHelper();
                        img.LoadImage(fileName);

                        images.Add(fileName, img);

                        return img;
                    }

                    return images[fileName];
                }
                else
                {
                    ImagingHelper img = new ImagingHelper();
                    img.LoadImage(fileName);

                    images.Add(fileName, img);

                    return img;
                }
            }
        }
        /// <summary>
        /// 从MzResV2.dll中加载图像资源
        /// </summary>
        /// <param name="resId">图像资源ID</param>
        /// <param name="useAlpha">是否启用图像Alpha通道</param>
        /// <returns>返回ImagingHelper对象</returns>
        public ImagingHelper LoadImageFromMzResV2(MzResV2 resId,bool useAlpha)
        {
            return LoadImageFromMzResV2((int)resId, useAlpha);
        }
        /// <summary>
        /// 从MzResV2.dll中加载图像资源
        /// </summary>
        /// <param name="resId">图像资源ID</param>
        /// <param name="useAlpha">是否启用图像Alpha通道</param>
        /// <returns>返回ImagingHelper对象</returns>
        public ImagingHelper LoadImageFromMzResV2(int resId, bool useAlpha)
        {
            IntPtr hInst = MzGraphicsAPI.GetMzResV2ModuleHandle();

            return LoadImage(hInst, resId, useAlpha);
        }
        /// <summary>
        /// 加载图像并返回此图像的ImagingHelper对象指针，此ImagingHelper对象同时被缓存在此ImageContainer对象中
        /// </summary>
        /// <param name="hInstance">保存图像的模块实例指针</param>
        /// <param name="resId">图像资源ID</param>
        /// <param name="useAlpha">是否启用图像Alpha通道</param>
        /// <returns>返回ImagingHelper对象</returns>
        public ImagingHelper LoadImage(IntPtr hInstance, int resId, bool useAlpha)
        {
            IntPtr handle = ImageContainerLoadImage(pObj, hInstance, resId, useAlpha);

            return new ImagingHelper(handle);
        }
        /// <summary>
        /// 从容器中删除所有已加载的图像 
        /// </summary>
        public void RemoveAll()
        {
            ImageContainerRemoveAll(pObj);
        }
        /// <summary>
        /// 从容器中删除指定的已加载的图像
        /// </summary>
        /// <param name="hInstance">保存图像的模块实例指针</param>
        /// <param name="resId">图像资源ID</param>
        /// <param name="useAlpha">是否启用图像Alpha通道</param>
        public void RemoveImage(IntPtr hInstance,int resId,bool useAlpha)
        {
            ImageContainerRemoveImage(pObj, hInstance, resId, useAlpha);
        }
        #endregion

        #region IDisposable 成员
        /// <summary>
        /// 销毁资源
        /// </summary>
        public void Dispose()
        {
            RemoveAll();

            ImageContainerDispose(pObj);
        }
        #endregion
    }
}
